//======================================================================
//
//	Cross-Platform XNA Menu System
//  http://cpxms.codeplex.com/
//
//  All code and content licensed under Microsoft Public License (Ms-PL)	
//
//======================================================================
//
//	File:		MainMenuComponent.cs
//
//	Author:		MJP
//
//	Date:		05/17/09
//
//======================================================================

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using CPXMenu;

namespace CPXMenuSample
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class MainMenuComponent : DrawableGameComponent
    {
        private MenuProject mainMenu;
        private SpacePongGame game;
        private Label sensitivityLabel;

        private int sensitivity = 5;
        private bool inverted = false;
		private bool subtitles = false;

		/// <summary>
		/// Creates a new instance of MainMenuComponent
		/// </summary>
		/// <param name="game"></param>
        public MainMenuComponent(Game game)
            : base(game)
        {
            this.game = (SpacePongGame)game;
        }

		/// <summary>
		/// Loads content used by the component
		/// </summary>
        protected override void LoadContent()
        {
			// Load up our MenuProject from the ContentManager
            mainMenu = Game.Content.Load<MenuProject>("Menus\\Startup");

			// Here we look for certain MenuItem's by name, and hook up to their events
			// so we can implement the reuqired logic.
            Button exitButton = mainMenu.GetMenuItemByName<Button>("ExitButton");
            exitButton.Clicked += new MenuItemDelegate(exitButton_Clicked);

            Checkbox invertedCheckbox = mainMenu.GetMenuItemByName<Checkbox>("InvertedCheckbox");
            invertedCheckbox.IsChecked = inverted;
            invertedCheckbox.Checked += new MenuItemDelegate(invertedCheckbox_Checked);
            invertedCheckbox.Unchecked += new MenuItemDelegate(invertedCheckbox_Unchecked);

            Button sensitivityUpButton = mainMenu.GetMenuItemByName<Button>("SensitivityUpButton");
            sensitivityUpButton.Clicked += new MenuItemDelegate(sensitivityUpButton_Clicked);
            Button sensitivityDownButton = mainMenu.GetMenuItemByName<Button>("SensitivityDownButton");
            sensitivityDownButton.Clicked += new MenuItemDelegate(sensitivityDownButton_Clicked);

            sensitivityLabel = mainMenu.GetMenuItemByName<Label>("SensitivityLabel");
            sensitivityLabel.Text.Text = sensitivity.ToString();

			Checkbox subtitlesCheckbox = mainMenu.GetMenuItemByName<Checkbox>("SubtitlesCheckbox");
			subtitlesCheckbox.IsChecked = subtitles;
			subtitlesCheckbox.Checked += new MenuItemDelegate(subtitlesCheckbox_Checked);
			subtitlesCheckbox.Unchecked += new MenuItemDelegate(subtitlesCheckbox_Unchecked);

            base.LoadContent();
        }

		void subtitlesCheckbox_Unchecked(MenuItem item)
		{
			subtitles = false;
		}

		void subtitlesCheckbox_Checked(MenuItem item)
		{
			subtitles = true;
		}

        void sensitivityDownButton_Clicked(MenuItem item)
        {
            sensitivity--;
            if (sensitivity < 1)
                sensitivity = 1;

            sensitivityLabel.Text.Text = sensitivity.ToString();
        }

        void sensitivityUpButton_Clicked(MenuItem item)
        {
            sensitivity++;
            if (sensitivity > 10)
                sensitivity = 10;

            sensitivityLabel.Text.Text = sensitivity.ToString();
        }

        void invertedCheckbox_Unchecked(MenuItem item)
        {
            inverted = false;
        }

        void invertedCheckbox_Checked(MenuItem item)
        {
            inverted = true;
        }

        void exitButton_Clicked(MenuItem item)
        {
            Game.Exit();
        }

        /// <summary>
        /// Updates the component
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
			// We'll just use default input, provided by the MenuInput class.
			// If we wanted we could use customized key bindings based on options
			// or locality.  We could even use alternative input devices.
            MenuInput input;
#if WINDOWS
            input = MenuInput.GetCombinedInput(game.GamepadIndex);
#else
            input = MenuInput.GetGamepadInput(game.GamepadIndex);
#endif

			// This needs to be called to allow the project to update itself.
            mainMenu.UpdateProject(input, gameTime.ElapsedGameTime.Milliseconds / 1000.0f);

            base.Update(gameTime);
        }

		/// <summary>
		/// Draws the component
		/// </summary>
		/// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
			// We call this to give the project a chance to render itself.
            mainMenu.RenderProject(gameTime.ElapsedGameTime.Milliseconds / 1000.0f);

            base.Draw(gameTime);
        }
    }
}